package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.PageResult;
import com.xique.door.bean.domain.Repair;
import com.xique.door.mapper.RepairMapper;
import com.xique.door.service.IRepairService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements IRepairService {

  @Override
  public PageResult getRepairList(
      Long parkId,
      Integer pageNum,
      Integer pageSize,
      String name,
      String status,
      String startTime,
      String endTime) {
    QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("park_id", parkId);
    queryWrapper.orderByDesc("create_time");
    if (name != null && !"".equals(name)) {
      queryWrapper.like("name", name);
    }
    if (status != null && !"".equals(status)) {
      queryWrapper.eq("status", status);
    }
    if (startTime != null && endTime != null && !"".equals(startTime) && !"".equals(endTime)) {
      queryWrapper.between("create_time", startTime, endTime);
    }
    if (startTime != null && !"".equals(startTime)) {
      queryWrapper.ge("create_time", startTime);
    }
    Page<Repair> page = new Page<>();
    page.setCurrent(pageNum);
    page.setSize(pageSize);
    IPage<Repair> list = baseMapper.selectPage(page, queryWrapper);
    long totalPages = list.getPages();
    long totalSize = list.getTotal();
    return PageResult.builder()
        .pageNum(pageNum)
        .pageSize(pageSize)
        .totalPages(totalPages)
        .totalSize(totalSize)
        .list(list.getRecords())
        .build();
  }
  /** 修改报修 */
  @Override
  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  public R updateRepair(Repair repair) {
    String str = repair.getIds();
    String[] s = str.split(",");
    if (s.length > 0) {
      for (String s1 : s) {
        repair.setId(Long.valueOf(s1));
        repair.setUpdateTime(new Date());
        baseMapper.updateById(repair);
      }
      return R.ok(null, "处理成功");
    } else {
      repair.setUpdateTime(new Date());
      repair.setUserId(SecurityUtils.getUserId());
      int rest = baseMapper.updateById(repair);
      if (rest > 0) {
        return R.ok(null, "处理成功");
      } else {
        return R.fail("处理失败");
      }
    }
  }

  @Override
  public Repair getRepair(Long id) {
    Repair repair = baseMapper.selectById(id);
    String str = repair.getImages();
    String[] image = str.split(",");
    repair.setImage(image);
    return repair;
  }

  @Override
  public R insertRepair(Repair repair) {
    repair.setCreateTime(new Date());
    repair.setUpdateTime(new Date());
    int rest = baseMapper.insert(repair);
    if (rest > 0) {
      return R.ok(null, "添加成功");
    } else {
      return R.fail("添加失败");
    }
  }

}
